package com.aiguigu.Collection;

import com.sun.javafx.collections.MappingChange;

import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

public class ListTest {
    public static void main(String[] args) {
/*       ArrayList list = new ArrayList();

       list.add("1");
       list.add("2");
       list.add("3");
       list.add("4");
       list.add("5");
       list.add("6");
       ArrayList list1 = (ArrayList) list.subList(2,4);*/
       LockSupportDemo lockSupportDemo = new LockSupportDemo();
       lockSupportDemo.te();
        CountDownLatch downLatch = new CountDownLatch(6);

       /* System.out.println(list);*/
        /*boolean contains = list.contains("1");
        System.out.println(list);*/



        /*ArrayList list1 = new ArrayList();
        list1.add("红楼");
        list1.add("梦");
        list.addAll(list1);
        ListIterator listIterator = list.listIterator();
        while (listIterator.hasNext()){
            Object next = listIterator.next();
            System.out.println(next);
        }
        System.out.println(list);
     */
        /*
        *
        * */

/*
        String string = new StringBuilder("meituan").append("meituan").toString();
        System.out.println(string);
        System.out.println(string.intern());
        System.out.println(string == string.intern());
        */
/*字符串常量池中有一个等于此string对象的字符串，则返回这个对象字符串的引用，否则，将此String对象包含的字符串添加到常量池中，返回引用*//*



        String string2 = new StringBuilder("ja").append("va").toString();
        */
/*java创建时sun.misc.version 会创建一个java字符串对象*//*

        System.out.println(string2);
        System.out.println(string2.intern());
        System.out.println(string2 == string2.intern());
*/

    }

    public static int[] twoSum(int[] nums,int target){
        Map<Integer, Integer> map = new HashMap<>();
        for (int i=0;i<nums.length;i++){
            int tempNum = target - nums[i];
            if (map.containsKey(tempNum)){
                return new int[]{map.get(tempNum),i};
            }
            map.put(nums[i],i);
        }
        return null;
    }
}
class LockSupportDemo{
    static Object object = new Object();
    public void Ters(){
        new Thread(()->{
            synchronized (object){
                System.out.println(Thread.currentThread().getName()+"come in");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            //必须有sync关键字，必须先wait 后notify 不然会一直wait
            System.out.println(Thread.currentThread().getName()+"被唤醒");
        },"aaa").start();

        new Thread(()->{
            synchronized (object){
                object.notify();
                System.out.println("通知");
            }
        },"bbb").start();
    }
    public void LockTests(){
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        new Thread(()->{
             lock.lock();
             try {
                 System.out.println(Thread.currentThread().getName()+"come in");
                 condition.await();
             }catch (Exception e){
                 e.printStackTrace();
             }finally {
                 lock.unlock();
             }
        },"aaa").start();
         //同样问题
        new Thread(()->{
            lock.lock();
            try {
                condition.signal();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        },"bbb").start();
    }

    public void te(){
        Thread a = new Thread(()->{

            System.out.println(Thread.currentThread().getName()+"come in");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName()+"唤醒");
        },"aaa");
        a.start();

        Thread b = new Thread(()->{
            LockSupport.unpark(a);
            System.out.println(Thread.currentThread().getName()+"通知");
        },"bbb");
        b.start();
    }
}
